<?php
/**
 * ==============================================
 * Copy right 2015-2016 http://wei.meididi88.com
 * ----------------------------------------------
 * This is not a free software, without any authorization is not allowed to use and spread.
 * ==============================================
 * @author: hy
 * @date: 2015-9-10
 * @version: v1.0.0
 */
namespace Home\Model;

/**
 * 订单评论Model模型类
 */
class OrderCommentModel extends CommentsModel{

	public $avg_score;
	
	public function autoComplete() {
		return C('DB_PREFIX').'order';
	}
	
	/**
	 * 订单评价
	 * 
	 * @return boolean
	 */
	public function addComment() {
		$this->startTrans();//开启事务
		
		if (!$this->create()) {
			return false;
		} else {
			$commentId = $this->add();
			if (!$commentId) {
				$this->rollback();
				return false;
			}
			
			$number = I('post.number');
			$photos = I('post.comment_photo');
			if ($photos) {
				$commentUrl = C('WX_MEDIA_DATA').$number.'/';
				$commentPhoto = array('comment_id' => $commentId, 'created' =>  time());
				$sort = 0;
				$CommentPhotoModel = D('comment_photo');
				foreach ($photos as $key => $value) {
					$img_name = saveMedia($value, $commentUrl);
					++$sort;
					$commentPhoto ['sort'] = $sort;
					$commentPhoto ['img_name'] = $img_name;
					$commentPhoto ['upload_number'] = $number;
					$commentPhoto ['service_id'] = $value;
					$commentPhotoId = $CommentPhotoModel->add($commentPhoto);
					if (!$commentPhotoId) {
						$this->rollback();
						$this->error = '保存评论图片失败';
						return false;
					}
				}
			}
			
			if (!I('post.parentid')) {
				$BeauticianModel = D('Beautician');
				$beautician_id = I('post.to_uid');
				$beautician = $BeauticianModel->getBeauticianInfo($beautician_id);
				
				$BeauticianModel->updateScore($beautician_id, array(
					'professional_score' => $beautician ['professional_score'] + I('post.professional'),
					'punctuality_score' => $beautician ['punctuality_score'] + I('post.punctuality'),
					'communication_score' => $beautician ['communication_score'] + I('post.communication'),
					'comment_total' => $beautician ['comment_total'] + 1,
				));
				
				$OrderModel = D('Order');
				
				$Order = $OrderModel->getOrderInfo(I('post.post_id'));
				$BeautyParlorModel = D('BeautyParlor');
				$beauty_id = $Order ['beauty_id'];
				$beauty = $BeautyParlorModel->getBeautyInfo($beauty_id);
				
				$OrderModel->where(array('id' => $Order['id']))->save(array('iscomment' => 1));
				$OrderModel->clearCache($Order['id']);
				
				$BeautyParlorModel->updateScore($beauty_id, array(
					'professional_score' => $beauty ['professional_score'] + I('post.professional'),
					'punctuality_score' => $beauty ['punctuality_score'] + I('post.punctuality'),
					'communication_score' => $beauty ['communication_score'] + I('post.communication'),
					'comment_total' => $beauty ['comment_total'] + 1,
				));
			}
		}
		
		$this->commit();
		return true;
	}
	
	/**
	 * 检测用户是否已评论该订单
	 * 
	 * @param number $orderId	订单编号
	 * @param number $uid		评价者
	 * 
	 * @return array|boolean
	 */
	public function checkCommentStatus($orderId, $uid) {
		return $this->where(array('post_id' => $orderId, 'uid' => $uid))->find();
	}
	
	/**
	 * 根据评论编号获取评论详细信息
	 * 
	 * @param number $commentId	评价编号
	 * @return boolean|array
	 */
	public function getCommentInfo($commentId) {
		$cacheKey = 'Comment_'.$commentId;
		$commentInfo = S($cacheKey);
		if (!$commentInfo) {
			$commentInfo = $this->where(array('id' => $commentId))->find();
			S($cacheKey, $commentInfo);
		}
		
		return $commentInfo;
	}
	
	/**
	 * 查询某用户对订单的评论详细信息
	 * 
	 * @param number $orderId	订单编号
	 * @param number $uid		评价者编号
	 * @return boolean|array
	 */
	public function getCommentByOrderIdAndUid($orderId, $uid) {
		return $this->where(array('post_table' => C('DB_PREFIX').'order', 'post_id' => $orderId, 'uid' => $uid))->find();
	}
	
	/**
	 * 组装查询条件
	 * 
	 * @param array $where
	 * @return array
	 */
	public function conbineWhere($where) {
		if ($this->avg_score) {
			$where ['avg_score'] = $this->avg_score;
		}
		
		return $where;
	}
	
	/**
	 * 检测是否评论过
	 * 
	 * @param number $user_id	用户编号
	 * @param number $post_id	订单编号
	 * @return boolean|number
	 */
	public function getUserOrderComment($user_id, $post_id) {
		return $this->where(array('uid' => $user_id, 'status' => 1, 'post_id' => $post_id))->find();
	}
	
	/**
	 * 查询美容院评论
	 * 
	 * @param number $beautyId	美容院编号
	 * @param number $offset	查询起始位置
	 * @param number $limit		查询量
	 * @return boolean|array
	 */
	public function getBeautyComments($beautyId, $offset = 0, $limit = 8) {
		$where = array('o.beauty_id' => $beautyId, 'c.parentid' => 0);
		$where = $this->conbineWhere($where);
		$where ['c.status'] = 1;
		
		return $this->field('c.*')->alias('c')->join('left join '.C('DB_PREFIX').'order as o on c.post_id=o.id')
			->where($where)
			->order('c.createtime DESC')
			->limit($offset.','.$limit)
			->select();
	}
	
	/**
	 * 根据美容师编号获取美容师的评价
	 * 
	 * @param unknown $beauticianId	美容师编号
	 * @param number $offset		查询其实位置
	 * @param number $limit			查询量
	 * @return boolean|array
	 */
	public function getBeauticianComments($beauticianId, $offset = 0, $limit = 8) {
		$where = array('to_uid' => $beauticianId, 'parentid' => '0');
		$where = $this->conbineWhere($where);
		$where ['status'] = 1;
		
		return $this->where($where)->order('createtime DESC')->limit($offset.','.$limit)->select();
	}
	
	/**
	 * 获取评论内容的图片
	 *
	 * @param number $commentId	评价编号
	 * @return boolean|array
	 */
	public function getCommentPhoto($commentId) {
		$cacheKey = 'Comment_Id_'. $commentId;
		$photos = S($cacheKey);
	
		if (!$photos) {
			$photos = M('comment_photo')->where(array('comment_id' => $commentId))->select();
			S($cacheKey, $photos);
		}
	
		return $photos;
	}
	
	/**
	 * 统计美容院的评论
	 * @param number $beautyId	美容院编号
	 * @return number
	 */
	public function totalBeautyComments($beautyId) {
		$where = array('o.beauty_id' => $beautyId, 'c.parentid' => 0);
		$where = $this->conbineWhere($where);
		$where ['c.status'] = 1;
		return $this->alias('c')->join('left join '.C('DB_PREFIX').'order as o on c.post_id=o.id')
			->where($where)
			->count();
	}
	
	
	/**
	 * 统计美容师评论数
	 * @param number $beauticianId	美容师编号
	 * @return number
	 */
	public function totalBeauticianComment($beauticianId) {
		$where = array('to_uid' => $beauticianId, 'parentid' => 0);
		$where = $this->conbineWhere($where);
		$where ['status'] = 1;
		return $this->where($where)->count();
	}
}

?>